package ee.fujitsu.handlers;
/**
 * @author Andrei
 *
 * 10.08.2012
 */

import java.io.PrintWriter;

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;

import ee.fujitsu.common.Printer;



/**
 * The Class HelpHandler.
 */
public class HelpHandler {

	/** The Constant ADD_HELP1. */
	private static final String ADD_HELP1 = "added order for one cusomer\n";
	
	/** The Constant ADD_HELP2. */
	private static final String ADD_HELP2 = "customer choosen randomly from list\n";
	
	/** The Constant ADD_HELP3. */
	private static final String ADD_HELP3 = "parametered film and day quantity\n";
	
	/** The Constant LATE_HELP1. */
	private static final String LATE_HELP1 = "added order for delayed films\n";
	
	/** The Constant LATE_HELP2. */
	private static final String LATE_HELP2 = "customer choosen randomly from list\n";
	
	/** The Constant LATE_HELP3. */
	private static final String LATE_HELP3 = "parametered day delay quantity\n";
	
	/** The Constant BONUS_HELP1. */
	private static final String BONUS_HELP1 = "added order for cusomer,\n";
	
	/** The Constant BONUS_HELP2. */
	private static final String BONUS_HELP2 = "who has enouth bonuses for pay\n";
	
	/** The Constant BONUS_HELP3. */
	private static final String BONUS_HELP3 = "at leastnone day rent\n";
	
	/** The Constant BONUS_HELP4. */
	private static final String BONUS_HELP4 = "parametered day quantity\n";
	
	/** The Constant INV_HELP1. */
	private static final String INV_HELP1 = "perform inventory actions\n";
   
   /** The Constant MAIN_HELP. */
   private static final String MAIN_HELP = "print detailed message"; 
   
   /**
    * Create output for help information
    * 
    * Use {@link #handle()} for detailed help.
    */
	@SuppressWarnings("static-access")
   public void handle() {
      Options filmOptions = new Options();
      OptionGroup optionGroup = new OptionGroup();
      optionGroup.addOption(new Option("help", MAIN_HELP));
      Option add = OptionBuilder
    	.withArgName( "film_qty" )
               .hasArg()
              .withDescription( ADD_HELP1 + ADD_HELP2 + ADD_HELP3)
              .create( "add" );
      optionGroup.addOption(add);
      Option late = OptionBuilder
      .withArgName( "day_qty" )
              .hasArg()
              .withDescription( LATE_HELP1 + LATE_HELP2 + LATE_HELP3)
              .create( "late" );
      optionGroup.addOption(late);
      Option bonus = OptionBuilder
      .withArgName( "day_qty" )
              .hasArg()
              .withDescription( BONUS_HELP1 + BONUS_HELP2 + BONUS_HELP3 + BONUS_HELP4)
              .create( "bonus" );
      optionGroup.addOption(bonus);
     Option invent = OptionBuilder
     .withArgName( "action type" )
             .hasArg()
             .withDescription( INV_HELP1)
             .create( "invent" );
     optionGroup.addOption(invent);
      filmOptions.addOptionGroup(optionGroup);
      printHelp(
    		  filmOptions, // options for this help
            80, // output width
            "Options", // line before help
            "-- HELP --", // line after help
            3, // spaces before option 
            5, // spaces before option describe
            false // is it heeded commands output
        );
   }

   /**
    * Create output for help information
    * 
    * Use {@link #handle()} for short help just how to use.
    *
    * @param string the string
    */
   public void handle(String string) {
      Options filmOptions = new Options();
      OptionGroup optionGroup = new OptionGroup();
      optionGroup.addOption(new Option("help", MAIN_HELP));
      optionGroup.addOption(new Option("add", true, ADD_HELP1));
      optionGroup.addOption(new Option("late", true, ADD_HELP1));
      optionGroup.addOption(new Option("bonus", true, ADD_HELP1));
      optionGroup.addOption(new Option("invent", true, ADD_HELP1));
      filmOptions.addOptionGroup(optionGroup);
      printHelp( filmOptions, 80, "Options", "-- SHORT HELP --", 3,  5,  false);
   }
   
   /**
    * Prints the help.
    *
    * @param options the options
    * @param printedRowWidth the printed row width
    * @param header the header
    * @param footer the footer
    * @param spacesBeforeOption the spaces before option
    * @param spacesBeforeOptionDescription the spaces before option description
    * @param displayUsage the display usage
    */
   private void printHelp(final Options options, final int printedRowWidth, final String header, final String footer,
         final int spacesBeforeOption, final int spacesBeforeOptionDescription, final boolean displayUsage) {
      
      final String commandLineSyntax = "java -jar cinamon.jar -add 5 -add 3 \n or \n java -jar cinamon.jar -help"; // command line describe
      final PrintWriter writer = new PrintWriter(Printer.getStream());// where help printed
      final HelpFormatter helpFormatter = new HelpFormatter();// object for help output
      
      helpFormatter.printHelp(writer, printedRowWidth, commandLineSyntax, header, options, spacesBeforeOption,
            spacesBeforeOptionDescription, footer, displayUsage);// help description
      writer.flush(); // 
   }
}
